const path = require('path');
const webpack = require('webpack');
const dotenv = require('dotenv');

module.exports = (env, argv) => {
    const envFile = argv.mode === 'development' ? '.env.development' : '.env.production';
    dotenv.config({ path: envFile });
    
    return {
        entry: {
            background: './src/background.js',
            content: './src/content.js'
        },
        output: {
            filename: '[name].js',
            path: path.resolve(__dirname, 'dist')
        },
        mode: argv.mode,
        devtool: 'cheap-source-map',
        module: {
            rules: [
                {
                    test: /\.js$/,
                    exclude: /node_modules/,
                    use: {
                        loader: 'babel-loader',
                        options: {
                            presets: [
                                ['@babel/preset-env', {
                                    modules: false,
                                    targets: {
                                        chrome: "88"
                                    }
                                }]
                            ]
                        }
                    }
                },
                {
                    test: /\.css$/,
                    use: ['style-loader', 'css-loader', 'postcss-loader']
                }
            ]
        },
        optimization: {
            minimize: false,
            moduleIds: 'named',
            chunkIds: 'named'
        },
        resolve: {
            extensions: ['.js']
        },
        plugins: [
            new webpack.DefinePlugin({
                'process.env.NODE_ENV': JSON.stringify(argv.mode)
            })
        ]
    };
}; 